//
//  MediaLibraryAccess_03ViewController.m
//  MediaLibraryAccess_03
//
//  Created by 畑圭輔 on 10/02/07.
//  Copyright Keisuke Hata 2010. All rights reserved.
//

#import "MediaLibraryAccess_03ViewController.h"

@interface MediaLibraryAccess_03ViewController( Private )

- (void) alert:(NSString *)title message:(NSString *)message;

@end

@implementation MediaLibraryAccess_03ViewController

// アートワーク画像を取得する
-(IBAction) getArtwork {

	// 先頭の曲のコレクションを取得します。
	MPMediaItemCollection *collection = [_songs objectAtIndex:0];
	MPMediaItem* song = collection.representativeItem;

	// アートワークアイテムを取得します。
	MPMediaItemArtwork* artwork = [song valueForProperty:MPMediaItemPropertyArtwork];
	if( artwork == nil )
	{
		[self alert:@"エラー" message:@"アートワークが取得できませんでした"];
		return;
	}
	
	// 200x200のサイズでアートワークを取得します。
	UIImage *img = [artwork imageWithSize:CGSizeMake(200.0f, 200.0f)];
	_artWork.image = img;
}

- (void) alert:(NSString *)title message:(NSString *)message {
	UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK",nil] autorelease];
	[alert show];
}

/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void)viewDidLoad {
    [super viewDidLoad];

	// 曲のタイトルでグルーピングされたデータタイプが"Music"のクエリーを取得し、
	// コレクション情報を取得します。
	MPMediaQuery *songsQuery = [MPMediaQuery songsQuery];
	_songs = [[songsQuery collections] retain];
	NSAssert( _songs != nil , @"曲の取得に失敗しました" );
	isExecute = YES;
	
	int songCount = [_songs count];
	if( songCount == 0 )
	{
		[self alert:@"警告" message:@"曲が１曲も入っていないためサンプルを実行することはできません"];
		isExecute = NO;
		_songArtworkButton.enabled = NO;
	}
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[_songs release];
    [super dealloc];
}

@end
